package com.server.questionbank.snframe.service.CourseService.Impl.Member;

import com.server.questionbank.snframe.domain.Course.Member.CoursePermission;
import com.server.questionbank.snframe.domain.Course.Member.Permission;
import com.server.questionbank.snframe.domain.Course.Member.UserRegistCus;
import com.server.questionbank.snframe.mapper.Course.Member.MemberMapper;
import com.server.questionbank.snframe.mapper.TUserMapper;
import com.server.questionbank.snframe.service.CourseService.Member.CusMemberGroupService;
import com.server.questionbank.snframe.service.CourseService.Member.CusMemberService;
import com.server.questionbank.snframe.service.CourseService.TCourseService;
import com.server.questionbank.snframe.service.UsersService.UsersService;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class CusMemberServiceImpl implements CusMemberService {
    @Resource // 用户映射
    private UsersService usersService;
    @Autowired // 课程学生映射
    private MemberMapper memberMapper;
    @Autowired
    private CusMemberGroupService cusMemberGroupService;
    @Autowired
    private TCourseService tCourseService;

    // 加入课程
    @Override
    public Integer joinCourse(Long courseId, Long userId, String userNickName) {
        String nickName = userNickName;
        // 昵称为空，则取用户表中昵称
        if (userNickName == null || userNickName.isEmpty()) nickName = usersService.selectUserById(userId).getNickName();
        Long groupId = cusMemberGroupService.getDefaultCusStudyGroupByCourseId(courseId).getGroupId();
        return memberMapper.joinCourse(
                courseId, userId, groupId, nickName, "student", 1
        );
    }

    // 获取成员权限
    @Override
    public CoursePermission getMemberPermission(Long courseId, Long memberId) {
        CoursePermission permission = new CoursePermission();
        // 获取成员信息
        UserRegistCus userRegistCus = memberMapper.getCourseMember(courseId, memberId);
        if (userRegistCus != null) permission.setPermissionFlags(userRegistCus.getPermission());

        // 获取课程创建者
        Long createBy = tCourseService.getCourseById(courseId).getCreateBy();
        if (createBy.equals(memberId))
        {
            // 创建者拥有查看和编辑权限
            permission.addPermission(Permission.CAN_VIEW);
            permission.addPermission(Permission.CAN_EDIT);
        }
        return permission;
    }

    @Override
    public UserRegistCus getMemberInfo(Long courseId, Long memberId) {
        return memberMapper.getCourseMember(courseId, memberId);
    }

    // 获取课程成员
    @Override
    public List<UserRegistCus> getAllMember(Long courseId) {
        List<UserRegistCus> memberList = memberMapper.getCourseMemberList(courseId).stream().map(userRegistCus -> {
            userRegistCus.setUserInfo(usersService.selectUserById(userRegistCus.getUserId()));
            return userRegistCus;
        }).collect(Collectors.toList());
        return memberList;
    }

    @Override
    public List<UserRegistCus> getAllMember(Long courseId, Long groupId) {
        List<UserRegistCus> memberList = memberMapper.getCourseMemberListByGroupId(courseId, groupId).stream().map(userRegistCus -> {
            userRegistCus.setUserInfo(usersService.selectUserById(userRegistCus.getUserId()));
            return userRegistCus;
        }).collect(Collectors.toList());
        return memberList;
    }
}
